
"""
    组件表
"""


from django.db import models
from .asste import AssetModel


class CPUModel(models.Model):

    """
        CPU组件
    """

    asset = models.OneToOneField(AssetModel, on_delete=models.CASCADE)  # 设备上的cpu肯定都是一样的，所以不需要建立多个cpu数据，一条就可以，因此使用一对一。
    cpu_model = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'CPU型号')
    cpu_count = models.PositiveSmallIntegerField(default=1, verbose_name=u'物理CPU个数')
    cpu_core_count = models.PositiveSmallIntegerField(default=1, verbose_name=u'CPU核数')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'CPU'
        verbose_name_plural = verbose_name


class RAMModel(models.Model):
    """
        内存组件
    """

    asset = models.ForeignKey(AssetModel, on_delete=models.CASCADE)
    sn = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'SN号')
    model = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'内存型号')
    manufacturer = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'内存制造商')
    slot = models.CharField(max_length=64, verbose_name=u'插槽')
    capacity = models.IntegerField(blank=True, null=True, verbose_name=u'内存大小(GB)')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return '%s: %s: %s: %s' % (self.asset.name, self.model, self.slot, self.capacity)

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'内存'
        verbose_name_plural = verbose_name
        unique_together = ('asset', 'slot')  # (联合唯一) 同一资产下的内存，根据插槽的不同，必须唯一


class DiskModel(models.Model):

    """
        硬盘设备
    """

    disk_interface_type_choice = (
        (0, 'SATA'),
        (1, 'SAS'),
        (2, 'SCSI'),
        (3, 'SSD'),
        (4, 'unknown'),
    )

    asset = models.ForeignKey(AssetModel, on_delete=models.CASCADE)
    sn = models.CharField(max_length=128, verbose_name=u'硬盘SN号')
    slot = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'所在插槽位')
    model = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'磁盘型号')
    manufacturer = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'磁盘制造商')
    capacity = models.FloatField(blank=True, null=True, verbose_name=u'磁盘容量(GB)')
    interface_type = models.CharField(max_length=16, choices=disk_interface_type_choice, default='unknown',
                                      verbose_name=u'接口类型')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = '硬盘'
        verbose_name_plural = "硬盘"
        unique_together = ('asset', 'sn')


class NICModel(models.Model):

    """
        网卡组件
    """

    asset = models.ForeignKey(AssetModel, on_delete=models.CASCADE)
    name = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'网卡名称')
    model = models.CharField(max_length=128, verbose_name=u'网卡型号')
    mac = models.CharField(max_length=64, verbose_name=u'MAC地址')  # 虚拟机有可能会出现同样的mac地址
    ip_address = models.GenericIPAddressField(blank=True, null=True, verbose_name=u'IP地址')
    net_mask = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'掩码')
    bonding = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'绑定地址')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'网卡'
        verbose_name_plural = verbose_name
        unique_together = ('asset', 'model', 'mac')  # 资产、型号和mac必须联合唯一。防止虚拟机中的特殊情况发生错误。

